package com.tumanex.socialshare;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.tumanex.R;
import com.tumanex.dialog.Dialogs;
import com.tumanex.facebook.android.AsyncFacebookRunner;
import com.tumanex.facebook.android.AsyncFacebookRunner.RequestListener;
import com.tumanex.facebook.android.DialogError;
import com.tumanex.facebook.android.Facebook;
import com.tumanex.facebook.android.Facebook.DialogListener;
import com.tumanex.facebook.android.FacebookError;

/**
 * 
 * @author artemka
 * 
 */

public class FacebookShare {

	private Activity mActivity;
	private Facebook mFacebook;
	private AsyncFacebookRunner mAsyncRunner;

	private static final int ERROR_OPER = 2;
	private static final int SUCS_OPER = 1;
	private static final int PUBLISH_BT_ENABLE = 4;

	private Handler mHandler;

	public FacebookShare(Activity activity, Handler _hander) {

		mActivity = activity;
		mHandler = _hander;

		mFacebook = new Facebook(ConstantsApp.FB_APP_ID);
		mAsyncRunner = new AsyncFacebookRunner(mFacebook);

	}

	public void loginIn() {

		Log.d("facebookLogin", "StartLogin");

		mFacebook
				.authorize(
						mActivity,
						new String[] { "user_photos,publish_checkins,publish_actions,publish_stream" },
						new DialogListener() {

							public void onFacebookError(FacebookError arg0) {

								showAuthErrorDialog();
								Log.d("facebookLogin", "onFacebookError");
							}

							public void onError(DialogError arg0) {

								showAuthErrorDialog();
								Log.d("facebookLogin", "onError");
							}

							public void onComplete(Bundle arg0) {

								Log.d("facebookLogin", "onComplete");
								setEnablePublish();
							}

							public void onCancel() {

								Log.d("facebookLogin", "onCancel");
								showAuthErrorDialog();
							}
						});
	}

	public void Share(String message, Bitmap bitmap) {

		Log.d("facebookShare", "startShare");

		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		Bitmap bmpCompressed = Bitmap
				.createScaledBitmap(bitmap, 640, 480, true);

		bmpCompressed.compress(CompressFormat.JPEG, 100, bos);

		byte[] image = bos.toByteArray();

		Bundle params = new Bundle();
		params.putString(Facebook.TOKEN, mFacebook.getAccessToken());

		if (message != null) {

			params.putString("message", message);
		}

		params.putByteArray("picture", image);

		mAsyncRunner.request("me/photos", params, "POST",
				new RequestListener() {

					public void onMalformedURLException(
							MalformedURLException arg0, Object arg1) {

						Log.d("facebookShare", "nMalformedURLException");
						shareError();
					}

					public void onIOException(IOException arg0, Object arg1) {

						Log.d("facebookShare", "onIOException");
						shareError();
					}

					public void onFileNotFoundException(
							FileNotFoundException arg0, Object arg1) {

						Log.d("facebookShare", "onFileNotFoundException");
						shareError();
					}

					public void onFacebookError(FacebookError arg0, Object arg1) {

						Log.d("facebookShare", "onFacebookError");
						shareError();
					}

					public void onComplete(String arg0, Object arg1) {

						Log.d("facebookShare", "onComplete");
						shareSuccs();
					}
				}, null);
	}

	public boolean isSessionValid() {

		return mFacebook.isSessionValid();
	}

	public void setEnablePublish() {

		mHandler.sendEmptyMessage(PUBLISH_BT_ENABLE);
	}

	public void showAuthErrorDialog() {

		mHandler.post(new Runnable() {

			@Override
			public void run() {

				Dialogs errorDialog = new Dialogs((Context) mActivity);
				errorDialog.showErrorDialog(
						mActivity.getString(R.string.auth_error_tl),
						mActivity.getString(R.string.auth_error_mes));
			}
		});
	}

	public void shareSuccs() {

		mHandler.sendEmptyMessage(SUCS_OPER);
	}

	public void shareError() {

		mHandler.sendEmptyMessage(ERROR_OPER);
	}

}
